package com.cw.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import com.cw.action.LotteryAction;
import com.cw.po.Cpaction;
import com.cw.util.SwingUtil;

public class CpPopupMenu extends JPopupMenu implements Observer{
	// Vector otherItem = new Vector(5);

	private Font font = new Font("Dialog", Font.BOLD, 12);
	private ImageIcon imageIcon = null;

	private ImageIcon menuIcon = null;
	private MainPanel target;

	public CpPopupMenu(MainPanel tar, String label) {
		super(label);
		this.imageIcon = createImage(label);
		menuIcon = SwingUtil.getImageIcon("type.PNG");
		this.target = tar;
	}
	
	public void initMenu(Vector<Cpaction> acts, Map<Cpaction, LotteryAction> mapAct){
		removeAll();
		for(Cpaction act:acts){
			JMenuItem mi = new JMenuItem(mapAct.get(act));
			add(mi);
		}
	}

	private void addMenuItem(Vector<Action> buttonInfo) {
		// for (int i = 0; i < otherItem.size(); i++) {
		// remove((JMenuItem)otherItem.elementAt(i));
		// }
		removeAll();

		for (int i = 0; i < buttonInfo.size(); i++) {

			JMenuItem mi = new JMenuItem(buttonInfo.elementAt(i));
			// JMenuItem mi = new JMenuItem(new
			// cp.gui.action.operate.DnloadAction(buttonInfo[i][1],
			// new ImageIcon(SwingUtil.
			// getFiltedImage(buttonInfo[i][0],
			// Color.WHITE)),parent));

			// mi.setToolTipText(buttonInfo[i][2]);
			add(mi);
			// otherItem.addElement(mi);
//			target.initMenuAction(mi);

		}

		this.validate();
	}

	private ImageIcon createImage(String text) {
		char[] ch = text.toCharArray();
		BufferedImage bi = new BufferedImage(25, 1000,
				BufferedImage.TYPE_INT_ARGB);
		ImageIcon image = new ImageIcon(bi);
		Graphics2D g2d = bi.createGraphics();

		GradientPaint paint = new GradientPaint(0, 0, Color.GRAY.brighter(),
				30, 10, Color.DARK_GRAY.darker(), true);
		g2d.setPaint(paint);

		g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight());

		/*
		 * AffineTransform at = new AffineTransform(); at.rotate(-Math.PI / 2);
		 * 
		 * g2d.setTransform(at);
		 */
		g2d.setColor(Color.white);
		g2d.setFont(font);
		// g2d.drawString(text, -100, bi.getWidth());
		for (int i = 0; i < ch.length; i++) {
			g2d.drawChars(ch, i, 1, 5, g2d.getFontMetrics().getAscent()
					* (1 + i) + 5);
		}

		return image;
	}

	public Insets getInsets() {
		Insets insets = (Insets) super.getInsets().clone();
		insets.left += imageIcon.getIconWidth();
		return insets;
	}

	public void paint(Graphics g) {
		super.paint(g);
		if (imageIcon != null) {
			Insets insets = getInsets();
			g.drawImage(menuIcon.getImage(), insets.left
					- menuIcon.getIconWidth() - 5, insets.top, null);
			g.drawImage(imageIcon.getImage(), insets.left
					- imageIcon.getIconWidth(), insets.top
					+ menuIcon.getIconHeight(), null);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}

}
